package com.whoimi.admin.rest;

import com.whoimi.common.ResultInfo;
import com.whoimi.common.jdbc.JdbcPageQueryHelper;
import com.whoimi.admin.entity.vo.UserNoticeVO;
import com.whoimi.admin.rest.req.NoticePageQuery;
import com.whoimi.admin.service.NoticeService;
import com.whoimi.admin.service.UserNoticeService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author whoimi
 * @since 2025-07-29
 **/
@RestController
@RequestMapping("/api/v1/notices")
class UserNoticeRest {
    @Resource
    private UserNoticeService userNoticeService;
    @Resource
    private NoticeService noticeService;

    @Resource
    private JdbcPageQueryHelper jdbcPageQueryHelper;

    private static final Logger log = LoggerFactory.getLogger(UserNoticeRest.class);

    /**
     * 获取我的通知公告分页列表
     */
    @GetMapping("/my-page")
    public ResultInfo<Page<UserNoticeVO>> getMyNoticePage(NoticePageQuery queryParams, Pageable pageable) {
        log.info("queryParams:{},pageRequest {}", queryParams, pageable);
        queryParams.setUsername("admin");
        String sql = """
                            SELECT
                              un.username,
                              un.notice_id,
                              un.is_read,
                              un.create_time,
                              n.id,
                              n.title,
                              n.type,
                              n.content,
                              n.publish_time,
                              n.publisher,
                              n.`level`
                            FROM
                              wiic_admin.sys_user_notice un
                              JOIN wiic_admin.sys_notice n ON un.notice_id = n.id
                """;


        Page<UserNoticeVO> result = jdbcPageQueryHelper.buildPageWithSubquery(
                sql,
                queryParams,
                pageable,
                new BeanPropertyRowMapper<>(UserNoticeVO.class)
        );


        return ResultInfo.ok(result);
//        return ResultInfo.ok(userNoticeService.transPage(pageable));

    }
/*    public ResultInfo<Page<Notice>> getMyNoticePage(NoticePageQuery queryParams, Pageable pageable) {
        log.info("queryParams:{},pageRequest {}", queryParams, pageable);
        Page<Notice> page = noticeService.page(pageable);
        return ResultInfo.ok(page);
    }*/

    @PutMapping("/read-all")
    public ResultInfo<Long> readAll() {
        Long l = userNoticeService.markAsRead("admin");
        return ResultInfo.ok(l);
    }
}
